//
//  CQTBaseCardCollectionViewFlowLayout.swift
//  TSListDemo
//
//  Created by qian on 2020/11/6.
//  Copyright © 2020 dvlproad. All rights reserved.
//

import UIKit

class CQTBaseCardCollectionViewFlowLayout: UICollectionViewFlowLayout {
    fileprivate var numberOfItems : Int = 0
    private var layoutAttributesArray: [UICollectionViewLayoutAttributes] = []
    
    override init() {
        super.init()
        self.collectionView?.decelerationRate = UIScrollView.DecelerationRate.fast
        self.collectionView?.isPagingEnabled = true
        self.scrollDirection = .vertical
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func prepare() {
        super.prepare()
        var array : [UICollectionViewLayoutAttributes] = []
        
        self.numberOfItems = self.collectionView!.numberOfItems(inSection: 0)
        
        let itemCount = collectionView?.numberOfItems(inSection: 0)
        for i in 0 ..< itemCount! {
            let indexPath = IndexPath(item: i, section: 0)
            let attribute = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            
            let cellSize: CGSize = self.getCellSize();
            attribute.bounds.size = cellSize
            
            let cellCenterX = self.getCellCenterX()
            
            let scale : CGFloat = self.getCellScale(itemIndex: i, offset: .zero)
            let cellCenterY = self.getLastCellCenterY(itemIndex: i, scale: scale)
            //debugPrint("cellCenterY=\(cellCenterY)")
            
            
            attribute.center = CGPoint(x: cellCenterX, y: cellCenterY)
            attribute.transform = CGAffineTransform(scaleX: scale, y: scale)
            //debugPrint("attribute.frame = \(attribute.frame)")
            
            attribute.zIndex = 10000 - indexPath.row
            array.append(attribute)
        }
        self.layoutAttributesArray = array
    }
    
    func getCellSize() -> CGSize {
        let collectionViewWidth = self.collectionView!.bounds.width
        let collectionViewHeight = self.collectionView!.bounds.height
        
        let cellHeight: CGFloat = collectionViewHeight - 80
        let cellWidth: CGFloat = collectionViewWidth - 20
        
        return CGSize.init(width: cellWidth, height: cellHeight)
    }
    
    
    func getCellCenterX() -> CGFloat {
        let collectionViewWidth = self.collectionView!.bounds.width
        let cellCenterX = collectionViewWidth / 2.0
        return cellCenterX;
    }
    
    func getLastCellCenterY(itemIndex: NSInteger, scale:CGFloat) -> CGFloat {
        let collectionViewHeight: CGFloat = self.collectionView!.bounds.height
        let cellCenterY = collectionViewHeight/2
        return cellCenterY
    }
    
    //  override func prepareForTransition(from oldLayout: UICollectionViewLayout) {
    //
    //  }
    override var collectionViewContentSize: CGSize {
        return CGSize(width: self.collectionView!.bounds.width  , height: self.collectionView!.bounds.height)
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        return self.layoutAttributesArray[indexPath.row]
    }
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.layoutAttributesArray
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    
    // MARK: - Getter
    func getCellScale(itemIndex: NSInteger, offset: CGPoint = .zero) -> CGFloat {
        var scale : CGFloat = 1.0
        if itemIndex <= 2 {
            scale = 1.0 - CGFloat(itemIndex) * 0.1
        } else {
            scale = 0.8
        }
        
        let lastScale: CGFloat = self.__offsetScale(scale: scale, offset: offset)
        
        return lastScale
    }
    
    /// 移动offset时候的的应该变成的scale
    func __offsetScale(scale: CGFloat, offset: CGPoint = .zero) -> CGFloat {
        let maxOffset = min(max(abs(offset.x), abs(offset.y)), 100) //maxOffset最大只可能为100
        let changeScaleValue = maxOffset / 1000
        
        var lastScale: CGFloat = scale + changeScaleValue   // 拖动的时候其他卡片cell变大
        lastScale = min(lastScale, 1.0)                     // 拖动的时候其他卡片cell变大,但不能超过第一张的大小
        
        //debugPrint("itemIndex=\(itemIndex), maxOffset=\(maxOffset), lastScale = \(lastScale)")
        
        return lastScale
    }
}
